<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
       <el-form-item label="场站号" prop="fieldCode">
        <el-select v-model.trim="queryParams.fieldCode" placeholder="请选择场站号" clearable size="small" @change="fieldChange">
          <el-option v-for="dict in fieldInfos"  :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="卡口通道" prop="channelCode">
        <el-select v-model.trim="queryParams.channelCode" placeholder="请选择卡口通道" clearable size="small">
          <el-option v-for="dict in chnlInfos"  :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="businessType">
        <el-select v-model.trim="queryParams.businessType" placeholder="请选择业务类型" clearable size="small">
          <el-option v-for="dict in dict.type.sys_bill_business_type"  :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:transferChannelInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:transferChannelInfo:remove']"
        >删除</el-button>
      </el-col>
     <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:transferChannelInfo:export']"
        >导出</el-button>
      </el-col>
      -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transferChannelInfoList" @selection-change="handleSelectionChange"   stripe border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="场所名称" align="center" prop="fieldName" />
      <el-table-column label="通道代码" align="center" prop="channelCode" />
      <el-table-column label="通道名称" align="center" prop="channelName" />
      <el-table-column label="业务类型" align="center" prop="businessType">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_bill_business_type" :value="scope.row.businessType" />
          </template>
      </el-table-column>
      <el-table-column label="转场步骤" align="center" prop="stepCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.transfer_step" :value="scope.row.stepCode" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
           <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="showDetail(scope.row)"
            v-hasPermi="['system:transferChannelInfo:query']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改转场通道配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="right"  >
          <el-row>
            <el-col :span="12">
              <el-form-item label="监管货场" prop="fieldCode">
                <el-select v-model.trim="form.fieldCode" placeholder="请选择监管货场" @change="fieldChange">
                   <el-option v-for="dict in fieldInfos"  :key="dict.value" :label="dict.label" :value="dict.value"/>
                </el-select>
              </el-form-item>
            </el-col>
       		<el-col :span="12">
		        <el-form-item label="卡口通道" prop="channelCode">
		          <el-select v-model.trim="form.channelCode" placeholder="请选择通道代码">
		             <el-option v-for="dict in chnlInfos"  :key="dict.value" :label="dict.label" :value="dict.value"/>
		          </el-select>
		        </el-form-item>
       		</el-col>
       		<el-col :span="12">
		        <el-form-item label="业务类型" prop="businessTypes">
		          <el-select v-model.trim="form.businessTypes" multiple placeholder="请选择业务类型">
		            <el-option v-for="dict in dict.type.sys_bill_business_type"  :key="dict.value" :label="dict.label" :value="dict.value" />
		          </el-select>
		        </el-form-item>
       		</el-col>
          <el-col :span="12">
            <el-form-item label="转场步骤" prop="stepCode">
              <el-select v-model.trim="form.stepCode" placeholder="请选择转场步骤">
                <el-option v-for="dict in dict.type.transfer_step"  :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
	  </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详情dialog-->
    <el-dialog :visible.sync="detailOpen" width="50%" append-to-body :close-on-click-modal="false">
       <el-descriptions class="margin-top" title="转场通道配置详情" :column="2"  border>
		 <el-descriptions-item>
		        <template slot="label">
		         通道代码
		        </template>
		       {{form.channelCode}}
		  </el-descriptions-item>
		 <el-descriptions-item>
		        <template slot="label">
		         业务类型
		        </template>
		       {{form.businessType}}
		  </el-descriptions-item>
		 <el-descriptions-item>
		        <template slot="label">
		         转场代码
		        </template>
		       {{form.stepCode}}
		  </el-descriptions-item>
         </el-descriptions>
        <div slot="footer" class="dialog-footer" style="text-align: center;">
          <el-button @click="closeDetail">关闭</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTransferChannelInfo, getTransferChannelInfo, delTransferChannelInfo, addTransferChannelInfo, updateTransferChannelInfo, exportTransferChannelInfo } from "@/api/system/transferChannelInfo";
import {queryChannelInfoList} from "@/api/system/channelInfo";
import {selectFieldDictList} from "@/api/system/baseFieldInfo";
export default {
  name: "TransferChannelInfo",
  dicts:['sys_bill_business_type','transfer_step'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      //场所列表
      fieldInfos:[],
      //通道信息
      chnlInfos:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
       //详情显示的时候，表单编辑禁用
      isViewDisabled:false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 转场通道配置表格数据
      transferChannelInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
       //显示详情
      detailOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        channelCode: null,
        businessType: null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        fieldCode: [
          {required: true, message: "监管场所不能为空", trigger: "blur"},
        ],channelCode: [
          {required: true, message: "通道不能为空", trigger: "blur"},
        ],businessType: [
          {required: true, message: "业务类型不能为空", trigger: "blur"},
        ],
        stepCode: [
          {required: true, message: "转场步骤不能为空", trigger: "blur"},
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getFieldList();
  },
  methods: {
    /** 查询转场通道配置列表 */
    getList() {
      this.loading = true;
      listTransferChannelInfo(this.queryParams).then(response => {
        this.transferChannelInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //场所场所列表
    getFieldList(){
      selectFieldDictList("").then(res=>{
        this.fieldInfos=res;
      });
    },
    //通道列表
    getChannelList(fieldCode){
      queryChannelInfoList(fieldCode).then(res=>{
        this.chnlInfos=res;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        channelCode: null,
        businessType: null,
        stepCode: null,
        fieldCode:null,
        channelName:null,
        fieldName:null
      };
      this.chnlInfos=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isViewDisabled=false;
      this.open = true;
      this.title = "添加转场通道配置";
      if(this.fieldInfos.length==0){
        this.getFieldList();
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.load(id);
      this.title = "修改转场通道配置";
      this.open = true;
      if(this.fieldInfos.length==0){
        this.getFieldList();
      }
    },
    //关闭详情
    closeDetail(){
      this.detailOpen=false;
    },
     //显示详情
    showDetail(row, column, event){
        this.reset();
        //关闭表单编辑模式
        this.isViewDisabled=true;
        const id = row.id;
        this.load(id);
          //打开详情
        this.detailOpen=true;
    },
     /** 加载数据 */
    load(id){
	    getTransferChannelInfo(id).then(response => {
          this.form = response.data;
                  });
	},
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTransferChannelInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTransferChannelInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除转场通道配置编号为"' + ids + '"的数据项？').then(function() {
        return delTransferChannelInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    fieldChange(val) {
      this.queryParams.channelCode='';
     this.getChannelList(val);
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有转场通道配置数据项？').then(() => {
        this.exportLoading = true;
        return exportTransferChannelInfo(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>



<style type="text/css">
  .el-form-item .el-select {
      width: 100%;
    }
</style>
